package net.adetech.crj.web;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.adetech.crj.bean.WgrtbForm;
import net.adetech.crj.bean.ZggmtbForm;
import net.adetech.crj.bean.ZggmtbResult;
import net.adetech.crj.common.ParamsUtil;
import net.adetech.crj.entity.Announcement;
import net.adetech.crj.entity.Apply;
import net.adetech.crj.entity.Notice;
import net.adetech.crj.entity.User;
import net.adetech.crj.utils.ServiceUtil;
import net.adetech.framework.entity.ServiceRequest;
import net.adetech.framework.entity.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ApplyWebController {

    @Autowired
    ServiceUtil serviceUtil;

    private static Map<String ,String> remindImageMap = new HashMap<String, String>();


    /**
     * 中国公民填表页面
     * @return
     */
    @RequestMapping("/apply/page/add")
    public String addApplyPage(HttpSession session,Model model)
    {
        User user = (User) session.getAttribute("currentUser");

        if ("1".equals(user.getUserType()))
        {
            ServiceResponse response = serviceUtil.callService("notice.getByType","type",2);
            Notice notice = (Notice)response.getResults().get("notice");
            model.addAttribute("notice",notice);
            return "zggmtbRemind";
        }
        else {
            ServiceResponse response = serviceUtil.callService("notice.getByType","type",4);
            Notice notice = (Notice)response.getResults().get("notice");
            model.addAttribute("notice",notice);
            return "wgrtbRemind";
        }
    }


    /**
     * 中国公民填表页面
     * @return
     */
    @RequestMapping("/zggmtb/page")
    public String zggmtb(HttpSession session,Model model)
    {
        User user = (User) session.getAttribute("currentUser");
        String paramsJson = TemplateUtil.buildJsonParams("cUser",user);

        model.addAttribute("params",paramsJson);
        return  "zggmtb";
    }



    /**
     * 中国公民填表页面
     * @return
     */
    @RequestMapping("/zggmtb/view/page")
    public String zggmtbView(Model model, HttpSession session, String applyId)
    {
        model.addAttribute("applyId",applyId);


        return  "zggmtbView";
    }


    @RequestMapping("/apply/view/page")
    public String viewApply(Model model, HttpSession session, String applyId)
    {
        User user = (User) session.getAttribute("currentUser");
        Apply apply = null;
        if (applyId != null) {
            ServiceResponse response = serviceUtil.callService("apply.getApplyById", "applyId", applyId);
            Apply result = (Apply) response.getResults().get("apply");
            if (result.getApplyUserId().equals(user.getIdNumber())) {
                apply = result;
            }
        }
        String applyJson = "var apply = "+apply.getApplyDetail()+";";
        model.addAttribute("applyJson",applyJson);
        model.addAttribute("apply",apply);
        if ("A".equals(apply.getApplySummary()))
        {
            return  "zggmtbView";
        }
        else {
            return "wgrtbView";
        }
    }

    @RequestMapping("/apply/tips/page")
    public String applyTips(Model model, HttpSession session, String applyId)
    {
        User user = (User) session.getAttribute("currentUser");
        Apply apply = null;
        if (applyId != null) {
            ServiceResponse response = serviceUtil.callService("apply.getApplyById", "applyId", applyId);
            Apply result = (Apply) response.getResults().get("apply");
            if (result.getApplyUserId().equals(user.getIdNumber())) {
                apply = result;
            }
        }
        String applyJson = "var apply = "+JSON.toJSONString(apply)+";";
        model.addAttribute("applyJson",applyJson);
        return "applyTips";
    }

    /**
     * 外国人填表页面
     * @return
     */
    @RequestMapping("/wgrtb/page")
    public String wgrtb(){
        return  "wgrtb";
    }


    /**
     * 填表结果页面
     * @param sqh
     * @param tips
     * @param model
     * @return
     */
    @RequestMapping("/tbjg")
    public String tbjg(String sqh,String tips,Model model){
        System.out.println(tips);
        if (tips!=null)
        {
            List<String> tipsList = Arrays.asList(tips.split("___"));
            model.addAttribute("tipsList",tipsList);
        }
        model.addAttribute("sqh",sqh);
        return  "tbjg";
    }

    /**
     * 填表结果页面
     * @param sqh
     * @param tips
     * @param model
     * @return
     */
    @RequestMapping("/wgrTbjg")
    public String wgrTbjg(String sqh,String tips,Model model){
        model.addAttribute("sqh",sqh);
        return  "wgrTbjg";
    }



    /**
     * 申请列表页面
     * @return
     */
    @RequestMapping("/applyList")
    public String applyList(HttpSession session){

        User user = (User)session.getAttribute("currentUser");

        if (user.getIdNumber()!=null&&!"".equals(user.getIdNumber()))
        {
            return  "applyList";
        }
        else {
            if ("1".equals(user.getUserType()))
            {
                return "zggmtbRemind";
            }
            else {
                return "wgrtbRemind";
            }
        }


    }


    @RequestMapping(value = "/zggmtb/page/submit")
    public String zggmtbSubmit(String formJson, HttpSession session,Model model){

        ZggmtbForm form = JSON.parseObject(formJson,ZggmtbForm.class);

        if(form.getQwd()!=null&&form.getQwd().size()==1)
        {
            form.setQwd(Arrays.asList(form.getQwd().get(0).split(",")));
        }
        if(form.getSqlb()!=null&&form.getSqlb().size()==1)
        {
            form.setSqlb(Arrays.asList(form.getSqlb().get(0).split(",")));
        }



        User user = (User)session.getAttribute("currentUser");


        ServiceRequest request = new ServiceRequest();
        Map<String,Object> params = new HashMap<String, Object>();
        params.put("form",form);
        params.put("user",user);
        request.setParams(params);
        ServiceResponse response = serviceUtil.call("zggmtb.submit",request);

        ZggmtbResult result = (ZggmtbResult) response.getResults().get("result");

        Apply apply = (Apply)response.getResults().get("apply");
        model.addAttribute("result",result);
        model.addAttribute("apply",apply);


        return "tbjg";
    }



    @RequestMapping(value = "/wgrtb/page/submit", produces = "application/json")
    public String wgrtbSubmit(String formJson, HttpSession session,Model model) {

        WgrtbForm form = JSON.parseObject(formJson,WgrtbForm.class);
        User user = (User)session.getAttribute("currentUser");

        ServiceRequest request = new ServiceRequest();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("form", form);
        params.put("user",user);

        request.setParams(params);
        ServiceResponse response = serviceUtil.call("wgrtb.submit", request);
        System.out.println("args_wgr---->" + response.get("args") + "\n");
        System.out.println("response_wgr---->" + response.get("response") + "\n");
        ZggmtbResult result = (ZggmtbResult) response.getResults().get("result");
        Apply apply = (Apply)response.get("apply");
        model.addAttribute("apply",apply);
        return "wgrTbjg";
    }
}
